package com.ruoyi.ini.controller;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.mapper.SysUserPostMapper;
import com.ruoyi.system.mapper.SysUserRoleMapper;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.utils.UrlUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.ini.domain.XlkjIni;
import com.ruoyi.ini.service.IXlkjIniService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 事件Controller
 *
 * @author wang
 * @date 2022-02-08
 */
@RestController
@RequestMapping("/ini/ini")
public class XlkjIniController extends BaseController
{
    @Autowired
    private IXlkjIniService xlkjIniService;
    @Autowired
    private ISysDeptService sysDeptService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;
    @Autowired
    private SysUserPostMapper userPostMapper;

    @GetMapping("/dept/user")
    public AjaxResult user(){
        List<SysUser> sysUsers = userPostMapper.selectUserByPostId();
        return AjaxResult.success(sysUsers);
    }

    /**
     * 查询事件列表
     */
    @PreAuthorize("@ss.hasPermi('ini:ini:list')")
    @GetMapping("/list")
    public TableDataInfo list(XlkjIni xlkjIni)
    {
        startPage();

        Long userId1 = SecurityUtils.getUserId();
        if(userId1 == 112){
            xlkjIni.setRoleId(112L);
        }

        List<XlkjIni> list = xlkjIniService.selectXlkjIniList(xlkjIni);
        for(XlkjIni ini:list){
            //设置督办状态，查询网格员电话和名字，网格长电话和名字，协办部门电话和名字
            SysUser sysUser3 = sysUserService.selectUserById(ini.getUserId());
            ini.setUserPhone(sysUser3.getPhonenumber());
            ini.setUserName(sysUser3.getNickName());

            if(ini.getAssistDepartment() != null){
                SysUser sysUser1 = sysUserService.selectUserById(Long.valueOf(ini.getAssistDepartment()));
                if(sysUser1 != null){
                    ini.setXiePhone(sysUser1.getPhonenumber());
                    ini.setXieName(sysUser1.getNickName());
                }
            }
            //图片路径拼接
            if(ini.getImg() != null && ini.getImg().length()>0){
                ini.setImg(UrlUtils.getUrl() + ini.getImg());
            }
            //音频路径拼接
            if(ini.getAudio() != null && ini.getAudio().length() > 0){
                ini.setAudio(UrlUtils.getUrl() + ini.getAudio());
            }
            //所属网格名称回显
            String deptName = sysDeptService.selectDeptById(Long.valueOf(ini.getDepartmentId())).getDeptName();
            ini.setDepartmentName(deptName);
            //网格员名称回显
            SysUser sysUser2 = sysUserService.selectUserById(ini.getUserId());
            ini.setUsername(sysUser2.getNickName());
            if(ini.getAssist().equals("Y")){
                //是否需要该用户协助
                Long userId = SecurityUtils.getUserId();
                ini.setUserHelp(userId == Long.valueOf(ini.getAssistDepartment()));
                //设置协管部门人员
                ini.setAssistDepartmentName(sysUserService.selectUserById(Long.valueOf(ini.getAssistDepartment())).getNickName());
            }
            //判断是否需要该用户督办（该用户是否为督办部门）
            Long userId = SecurityUtils.getUserId();
            if(userId == 112){
                ini.setUserSupervise(true);
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出事件列表
     */
    @PreAuthorize("@ss.hasPermi('ini:ini:export')")
    @Log(title = "事件", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(XlkjIni xlkjIni)
    {
        List<XlkjIni> list = xlkjIniService.selectXlkjIniList(xlkjIni);
        ExcelUtil<XlkjIni> util = new ExcelUtil<XlkjIni>(XlkjIni.class);
        return util.exportExcel(list, "事件数据");
    }

    /**
     * 获取事件详细信息
     */
    @PreAuthorize("@ss.hasPermi('ini:ini:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        XlkjIni ini = xlkjIniService.selectXlkjIniById(id);
        //设置督办状态，查询网格员电话和名字，网格长电话和名字，协办部门电话和名字
        ini.setSuperviseStatus("Y");
        SysUser sysUser3 = sysUserService.selectUserById(ini.getUserId());
        ini.setUserPhone(sysUser3.getPhonenumber());
        ini.setUserName(sysUser3.getNickName());
//        Long parentId = sysDeptService.selectDeptById(Long.valueOf(ini.getDepartmentId())).getParentId();                   //查询到了部门id（镇）
//        SysUser sysUser = new SysUser();
//        sysUser.setDeptId(parentId);
//        List<SysUser> sysUsers = sysUserService.selectUserList(sysUser);
//        for(SysUser user:sysUsers){
//            Long aLong = sysUserRoleMapper.selectRoleIdByUserId(user.getUserId());
//            if(aLong == 100){
//                ini.setManagePhone(user.getPhonenumber());
//                ini.setManageName(user.getNickName());
//                break;
//            }
//        }
        SysDept sysDept = sysDeptService.selectDeptById(Long.valueOf(ini.getDepartmentId()));
        SysUser sysUser = new SysUser();
        sysUser.setDeptId(sysDept.getDeptId());
        List<SysUser> sysUsers = sysUserService.selectUserList(sysUser);
        for(SysUser user:sysUsers){
            Long aLong = sysUserRoleMapper.selectRoleIdByUserId(user.getUserId());
            if(aLong == 100){
                ini.setManageName(user.getNickName());
                ini.setManagePhone(user.getPhonenumber());
            }
        }

        if(ini.getAssistDepartment() != null){
            SysUser sysUser1 = sysUserService.selectUserById(Long.valueOf(ini.getAssistDepartment()));
            ini.setXiePhone(sysUser1.getPhonenumber());
            ini.setXieName(sysUser1.getNickName());
        }

        //所属网格名称回显
        String deptName = sysDeptService.selectDeptById(Long.valueOf(ini.getDepartmentId())).getDeptName();
        ini.setDepartmentName(deptName);
        //网格员名称回显
        SysUser sysUser2 = sysUserService.selectUserById(ini.getUserId());
        ini.setUsername(sysUser2.getNickName());
        if(ini.getAssist().equals("Y")){
            //是否需要该用户协助
            Long userId = SecurityUtils.getUserId();
            ini.setUserHelp(userId == Long.valueOf(ini.getAssistDepartment()));
            //设置协管部门人员
            ini.setAssistDepartmentName(sysUserService.selectUserById(Long.valueOf(ini.getAssistDepartment())).getNickName());
        }
        return AjaxResult.success(ini);
    }

    /**
     * 新增事件
     */
    @PreAuthorize("@ss.hasPermi('ini:ini:add')")
    @Log(title = "事件", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody XlkjIni xlkjIni)
    {
        return toAjax(xlkjIniService.insertXlkjIni(xlkjIni));
    }

    /**
     * 修改事件
     */
    @PreAuthorize("@ss.hasPermi('ini:ini:edit')")
    @Log(title = "事件", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody XlkjIni xlkjIni)
    {
        return toAjax(xlkjIniService.updateXlkjIni(xlkjIni));
    }

    /**
     * 删除事件
     */
    @PreAuthorize("@ss.hasPermi('ini:ini:remove')")
    @Log(title = "事件", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(xlkjIniService.deleteXlkjIniByIds(ids));
    }
}
